package com.example.http;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

/**
 * @program: sms
 * @description:
 * @author: zhanghz001
 * @create: 2020-11-04 13:44
 **/
public class MyHttpServletRequestWrapper extends HttpServletRequestWrapper {
    /**
     * Constructs a request object wrapping the given request.
     *
     * @param request
     * @throws IllegalArgumentException if the request is null
     */
    public MyHttpServletRequestWrapper(HttpServletRequest request) {
        super(request);

    }

    @Override
    public String getParameter(String name) {
        String value = super.getParameter(name);
        return htmlFilter(value);

    }

    private String htmlFilter(String value) {
        char[] chars = value.toCharArray();
        StringBuffer sb = new StringBuffer();
        for (char aChar : chars) {
            switch (aChar) {
                case '&':
                    sb.append("&amp;");
                    break;
                case '<':
                    sb.append("&lt;");
                    break;
                case '>':
                    sb.append("&gt;");
                    break;
                case '\"':
                    sb.append("&quot;");
                    break;
                default:
                    sb.append(aChar);
            }
        }
        return sb.toString();
    }

}
